package com.spoto.oa.service.impl;

import java.sql.SQLException;
import java.util.List;

import com.spoto.oa.dao.ProductDao;
import com.spoto.oa.dao.impl.ProductDaoImpl;
import com.spoto.oa.model.Employee;
import com.spoto.oa.model.Product;
import com.spoto.oa.service.ProductServ;

public class ProductServImpl implements ProductServ {

	ProductDao proDao = new ProductDaoImpl();

	public void addProduct(Product product) throws SQLException {
		proDao.save(product);
	}

	public void deleteProduct(Product product) {
		proDao.delete(product);
	}

	public List<Product> getAllProducts() {
		List<Product> products = proDao.getAllProduct();
		return products;
	}

	public Product selectByCode(String code) {
		Product product = proDao.selectByCode(code);
		return product;
	}

	public List<Product> searchByCode(String code) {
		List<Product> products = proDao.getByCode(code);
		return products;
	}

	public List<Product> searchByCompany(String company) {
		List<Product> products = proDao.getByCompany(company);
		return products;
	}

	public List<Product> searchByName(String name) {
		List<Product> products = proDao.getByName(name);
		return products;
	}

	public List<Product> searchByType(String type) {
		List<Product> products = proDao.getByType(type);
		return products;
	}

	public void updateProduct(int id, Product productNew) {
		proDao.update(id, productNew);
	}

	public List<Product> search(String searchType, String value) {
		if (searchType.equals("code")) {
			return searchByCode(value);
		} else if (searchType.equals("company")) {
			return searchByCompany(value);
		} else if (searchType.equals("name")) {
			return searchByName(value);
		} else {
			return searchByType(value);
		}
	}

	public List<Product> getAllConflict() {
		List<Product> products = proDao.getAllProductConflict();
		return products;
	}

	public Product getConflictByCode(String code) {
		Product product = proDao.selectConflictCode(code);
		return product;
	}

	public void saveConflict(Product product) throws SQLException {
		proDao.saveConfict(product);
	}

	public void deleteProductConflict(Product product) {
		proDao.deleteConflict(product);
	}

	public String[] getProductNamebyType(String type) {
		List<Product> tmpProducts = searchByType(type);
		int size = tmpProducts.size();
		String[] products = new String[size];
		for (int i = 0; i < size; i++) {
			products[i] = tmpProducts.get(i).getCode();
		}		
		return products;
	}
	
}
